package com.atguigu.spzx.order.controller;

import com.atguigu.spzx.model.dto.api.ApiOrderSaveDTO;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.order.ApiOrderTradeVO;
import com.atguigu.spzx.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author LT
 * @create 2024-03-29-19:42
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderController {

    @Resource
    OrderService orderService;

    @PutMapping("/auth/updatePayTypeAndOrderStatus/{payType}/{paymentStatus}/{orderNo}")
    public Result updatePayTypeAndOrderStatus(@PathVariable("payType")Integer payType,
                                              @PathVariable("paymentStatus") String paymentStatus,
                                              @PathVariable("orderNo") String orderNo){
        orderService.updatePayTypeAndOrderStatus(payType,paymentStatus,orderNo);
        return Result.ok();
    }

    @PutMapping("/auth/updateStockAndSale/{orderNo}")
    public Result updateStockAndSale(@PathVariable("orderNo") String orderNo){
        orderService.updateStockAndSale(orderNo);
        return Result.ok();
    }

    @GetMapping("/auth/getByOrderInfoOrderNo/{orderNo}")
    public Result<OrderInfo> getByOrderInfoOrderNo(@PathVariable("orderNo")Long orderNo){
        OrderInfo orderInfo = orderService.getByOrderInfoOrderNo(orderNo);
        return Result.ok(orderInfo);
    }


    @GetMapping("/auth/{id}")
    public Result getOrderById(@PathVariable("id")Long id){
        OrderInfo orderInfo = orderService.getById(id);
        return Result.ok(orderInfo);
    }

    @GetMapping("/auth/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       Integer orderStatus){
        PageInfo<OrderInfo> pageInfo = orderService.listByStatus(pageNum,pageSize,orderStatus);
        return Result.ok(pageInfo);
    }

    @PostMapping("/auth/submitOrder")
    public Result submitOrder(@RequestBody ApiOrderSaveDTO orderSaveDTO){
        Long orderId = orderService.submitOrder(orderSaveDTO);
        return Result.ok(orderId);
    }

    @GetMapping("/auth/trade")
    public Result trade(){
        ApiOrderTradeVO orderTradeVO = orderService.trade();
        return Result.ok(orderTradeVO);
    }

    @GetMapping("/auth/buy/{skuId}")
    public Result trade(@PathVariable("skuId")Long skuId){
        ApiOrderTradeVO orderTradeVO = orderService.buy(skuId);
        return Result.ok(orderTradeVO);
    }

}
